<template>
  <div class="chart-container">
    <div class="chart-header">
      <a-radio-group v-model:value="chartType" size="small" @change="updateChart">
        <a-radio-button value="count">人数</a-radio-button>
        <a-radio-button value="completion">完成率</a-radio-button>
      </a-radio-group>
    </div>
    <div ref="chartRef" class="chart"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Array,
    default: () => [
      { value: 121, name: '钢筋工' },
      { value: 98, name: '木工' },
      { value: 85, name: '泥工' },
      { value: 76, name: '电工' },
      { value: 62, name: '焊工' }
    ]
  }
})

const chartRef = ref(null)
const chartType = ref('count')
let chart = null

const chartOption = {
  tooltip: {
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  legend: {
    orient: 'vertical',
    left: 'left',
    textStyle: {
      color: '#fff'
    }
  },
  series: [
    {
      name: '工作分布',
      type: 'pie',
      radius: ['50%', '70%'],
      avoidLabelOverlap: false,
      itemStyle: {
        borderRadius: 10,
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        show: false,
        position: 'center'
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '20',
          fontWeight: 'bold'
        }
      },
      labelLine: {
        show: false
      },
      data: [
        { value: 121, name: '钢筋工' },
        { value: 98, name: '木工' },
        { value: 85, name: '泥工' },
        { value: 76, name: '电工' },
        { value: 62, name: '焊工' }
      ]
    }
  ]
}

const updateChart = () => {
  if (!chart) return
  
  const option = {
    ...chartOption,
    series: [{
      ...chartOption.series[0],
      data: chartType.value === 'count' 
        ? props.data.map(item => ({ value: item.count, name: item.type }))
        : props.data.map(item => ({ 
            value: Number(item.completion.replace('%', '')), 
            name: item.name 
          }))
    }]
  }
  chart.setOption(option)
}

onMounted(() => {
  chart = echarts.init(chartRef.value)
  chart.setOption(chartOption)
  
  // 添加窗口大小变化的监听
  window.addEventListener('resize', handleResize)
})

const handleResize = () => {
  chart?.resize()
}

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  chart?.dispose()
})
</script>

<style scoped>
.chart-container {
  background-color: rgba(255, 255, 255, 0.05);
  background-color: rgb(0 20 53 / 79%);
  border-radius: 6px;
  padding: 1.5vh;
  height: 55vh;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.chart-header h3 {
  margin: 0;
  color: #409EFF;
  font-size: 16px;
}

.chart {
  height: calc(100% - 5vh);
  width: 100%;
}

:deep(.ant-radio-button-wrapper) {
  background: transparent !important;
  border-color: rgba(255, 255, 255, 0.1) !important;
  color: #fff !important;
}

:deep(.ant-radio-button-wrapper-checked) {
  background: #409EFF !important;
  border-color: #409EFF !important;
}
</style> 